function setupWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }
    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }
    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'https://__bridge_loaded__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function () {
        document.documentElement.removeChild(WVJBIframe)
    }, 0)
}

function connectWebViewJavascriptBridge(callback) {
    if (window.WebViewJavascriptBridge) {
        callback(WebViewJavascriptBridge)
    } else {
        document.addEventListener(
            'WebViewJavascriptBridgeReady',
            function () {
                callback(WebViewJavascriptBridge)
            },
            false
        );
    }
}


connectWebViewJavascriptBridge(function (bridge) {
    bridge.init(function (message, responseCallback) {
        responseCallback(data);
    });

    bridge.registerHandler("functionInJs", function (data, responseCallback) {
        responseCallback(responseData);
    });
})

function saveData(mobile, password) {
    setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('ObjC Echo', {
            mobile: mobile,
            password: password
        }, function responseCallback(responseData) {
            console.log("JS received response:", responseData)
        })
    })
}

function saveDataAndroid(mobile, password) {
    setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('putUserAndPassword', `${mobile}:${password}`, function responseCallback(responseData) {
            console.log("JS received response:", responseData)
        })
    })
}

function clearData() {
    setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('Clear', null, function responseCallback(responseData) {
            console.log("JS received response:", responseData)
        })
    })
}

function clearDataAndroid() {
    setupWebViewJavascriptBridge(function (bridge) {
        bridge.callHandler('removeUserAndPassword', null, function responseCallback(responseData) {
            console.log("JS received response:", responseData)
        })
    })
}